package cn.bonoon.kernel.io;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class FileSize {
	
	private final Long size;
	
	public static FileSize valueOf(Long size){
		return new FileSize(size);
	}
	
	private FileSize(Long size){
		this.size = size;
	}

	public final static long K = 1024;
	public final static long M = K * K;
	public final static long G = M * K;
	public final static long T = G * K;
	
	private final static NumberFormat FORMAT = new DecimalFormat("0.##");
	
	@Override
	public String toString() {
		if(null == size) return "";
		
		if(size > T) return FORMAT.format(size.doubleValue() / T) + "T";
		if(size > G) return FORMAT.format(size.doubleValue() / G) + "G";
		if(size > M) return FORMAT.format(size.doubleValue() / M) + "M";
		if(size > K) return FORMAT.format(size.doubleValue() / K) + "K";
			
		return size + "b";
	}
	
	public long size(){
		return this.size;
	}
}
